package com.idi.maven.plugins;

import com.idi.remote.assistant.RemoteAccessProvider;

import java.util.*;

/**
 * Holds properties for the build taken from external properties files
 *
 * @author Yossi Shaul
 */
@SuppressWarnings({"ClassWithTooManyMethods"})
public class BuildProperties implements RemoteAccessProvider {
    private final Map<String, String> props;

    public BuildProperties(final Map<String, String> props) {
        this.props = new HashMap<String, String>(props);
    }

    public String get(String key) {
        return props.get(key);
    }

    public String getFirstEarDeployDir() {
        return get("ear.deploy.dir") + "/" + getEarRelativeDeployDir();
    }

    public String getFirstEarRootDir() {
        return get("ear.deploy.dir");
    }

    /**
     * @return second deploy dir if versioning is supported, otherwise null
     */
    public String getSecondEarDeployDir() {
        if (isVersioningSupported()) {
            return get("ear.secondary.deploy.dir") + "/" + getEarRelativeDeployDir();
        } else {
            return null;
        }
    }

    public String getSecondEarRootDir() {
        return get("ear.secondary.deploy.dir");
    }

    public String getDeployUserPassword() {
        final String password = get("deploy.user.password");
        if (password == null) {
            throw new RuntimeException("deploy.user.password is null. please specify a valid password");
        }
        return password;
    }

    public String getEarRelativeDeployDir() {
        return get("ear.relative.deploy.path");
    }

    public String getDeployUser() {
        return get("deploy.user");
    }

    public String getServerHost() {
        return getServerHosts().get(0);
    }

    public List<String> getServerHosts() {
        List<String> result = new ArrayList<String>();
        result.addAll(get("server.hosts") != null ? Arrays.asList(get("server.hosts").split(",")) : Arrays.asList(get("server.host")));

        return result;
    }

    public String getTerracottaServerHost() {
        return get("terracotta.server.host");
    }

    public String getTerracottaSecondServerHost() {
        return get("terracotta.second.server.host");
    }

    public String getFirstTerracottaDataRootDir() {
        return get("terracotta.data.dir");
    }

    public String getSecondTerracottaDataRootDir() {
        return get("terracotta.secondary.data.dir");
    }

    public String getBackupDir() {
        return get("backup.dir");
    }

    /**
     * @return True is we have current/new support on the server
     */
    public boolean isVersioningSupported() {
        return Boolean.valueOf(get("isVersioningSupported"));
    }

    /**
     * @return True if isMonitorShown is set to true
     */
    public boolean isMonitorShown() {
        return Boolean.valueOf(get("isMonitorShown"));
    }

    /**
     * @return The directory on the remote server for the current jnlp files
     */
    public String getClientRemoteDir() {
        return get("client.files");
    }

    /**
     * @return The directory on the remote server for the testing jnlp files.
     *         Valid only if isVersioningSupported=true
     */
    public String getClientForTestingRemoteDir() {
        return get("client.files.new.version.test");
    }

    /**
     * @return the network interface name if the first node
     */
    public String getFirstNodeNetworkInterface1() {
        return get("first.node.network.interface.1");
    }

    /**
     * @return the jndi server port
     */
    public String getLocalJndiServerPort() {
        return get("local.jndi.server.port");
    }

    /**
     * @return dbName
     */
    public String getDefaultDbName() {
        return get("jam.env." + getEnvironment());
    }

    /**
     * @return environment
     */
    public String getEnvironment() {
        return get("environment");
    }

    public String getServerType() {
        return get("server.type");
    }

    public String getRestartScriptDir() {
        return get("restart.script.dir");
    }

    public String getServerLogsLinksPath() {
        return get("server.logs.links.path");
    }

    public String getApacheHost() {
        return get("apache.host");
    }

    public String getApachePort() {
        return get("port");
    }

    public String getApacheClientDir() {
        return get("apache.client.dir");
    }

    public String getApacheClientTestDir() {
        return get("apache.client.test.dir");
    }

    public String getSupportedDeploys() {
        return get("supported.deploys");
    }
}
